import { App } from "../../src/App";
import { SocketParser, PkgData } from "../../src/core/Class";
import { EventConst } from "../../src/core/EventConst";
import { ProtocolCmd, ProtocolProxy, ProtocolNet, ProtocolMain } from "./Protocol";

export default class NetParser extends SocketParser{
    private _openID:string = '';
    private _mainProtocol:number = ProtocolMain.Unknow;
    private _gameSvrID:string = '';

    constructor(){
        super();
        this.isStrMsg = true;
    }

    protected onInit(proto:number,svrID:string){
        if (App.Util.isInvalid(proto)){
            console.error('Error data NetParser.onInit()');
            return ;
        }
        this._mainProtocol = proto;
        this._gameSvrID = svrID;
    }

    protected onDestroy(){
        this._openID = '';
    }

    protected onEncode(data:any):string{
        App.Event.emit(EventConst.LOCK_SCREEN,true);
        let _str = '';
        if (typeof(data) !== 'string'){
            let _data = data;
            if (data.Protocol2 !== ProtocolProxy.C2Proxy_ConnDataProto) {
                data.OpenId = this._openID;
                _data = {
                    Protocol: this._mainProtocol,
                    Protocol2: ProtocolProxy.C2Proxy_SendDataProto,
                    ServerID: this._gameSvrID,
                    Data: data,
                };
            } else _data.OpenId = this._openID;

            if (data.Protocol === ProtocolMain.Unknow){
                console.error('Main protocol is error.')
            }
    
            _str = JSON.stringify(_data);
        } else _str = data;
        data.Protocol !== ProtocolNet.GameNet_Proto && console.table(`SendSocket:${_str}`);
        return _str;
    }

    protected onDeconde(data:any):PkgData{
        if (App.Util.isInvalid(data)) return null;
        data = App.String.decode64(data); 
        let _dt = JSON.parse(data);
        // 心跳协议
        if (_dt.Protocol === ProtocolNet.GameNet_Proto && _dt.Protocol2 === ProtocolNet.Net_HeartBeatProto){
            App.Event.emit(EventConst.SOCKET_SYNCHRO_SVR_TIME, _dt.TimeStamp);
            return null;
        }

        let name = ProtocolCmd[_dt.Protocol2];
        // 链接代理
        if (_dt.Protocol === this._mainProtocol){
            if (_dt.Protocol2 === ProtocolProxy.Proxy2C_ConnDataProto){
                this._openID = _dt.OpenID;
                name = 'Proxy2C_ConnDataProto';
            }
        } else if (_dt.Protocol === ProtocolProxy.Proxy2G_SendDataProto){
            name = ProtocolProxy[_dt.Protocol2];
        } else if (_dt.Protocol === ProtocolNet.GameNet_Proto){
            name = ProtocolNet[_dt.Protocol2];
        }
        console.table(`${name}=${JSON.stringify(_dt)}`);
        return {proto:name,data:_dt};
    }

    protected onHeartbeat(){
        App.send({
            Protocol:ProtocolNet.GameNet_Proto,
            Protocol2:ProtocolNet.Net_HeartBeatProto,
            TimeStamp:null,
        });
    }

    protected onMessage(proto:string,data:any){
        if (!proto || proto.length < 1)return ;

        delete data.Protocol;
        delete data.Protocol2;
        
        let _bindViews = {
            GS2C_HallShopConfProto2:'Mall:hall',
            GS2C_HallMatkaGameProto2:'Truntable:hall',
            GS2C_HallSignInProto2:'Signin:hall',
            GS2C_HallClickOnJiuJiProto2:'Welfare:hall',
            GS2C_EntrGameConifgRoomProto2:'ChoseRoom:hall',
        };

        App.Event.emit(EventConst.LOCK_SCREEN,false);
        const view = _bindViews[proto];
        if (App.Util.isValid(view)){
            App.openView(view,()=>{
                App.Event.emit(proto,data);
            });
            return ;
        }
        
        App.Event.emit(proto,data);
    }
}